<?php
/**
 * @copyright   Copyright (c) 2017 https://www.sapixx.com All rights reserved.
 * @license     Licensed (http://www.apache.org/licenses/LICENSE-2.0).
 * @author      pillar<ltmn@qq.com>
 * 支付有礼
 */
namespace app\allwin\model;
use think\Model;
use app\allwin\model\Coupon;
use app\allwin\model\CouponUser;
use app\allwin\model\AllwinGiftStore;
use app\allwin\model\Vip;
use app\common\facade\Inform;
use think\facade\Request;

class AllwinGiftData extends Model{

    protected $pk = 'id';
    
    //关联用户信息
    public function user(){
        return $this->hasOne('app\common\model\SystemUser','id','uid');
    }

    //所属活动
    public function gift(){
        return $this->hasOne('AllwinGift','id','gift_id');
    }

    /**
     * 付款后赠送给某个用户礼物(注意是在买单付款通知中实现)
     * @path  allwin\controller\v4\Notify\storeWechat();
     * @return Boolean
     */
    public static function giveGift($order,$miniapp_id){
        $store = AllwinGiftStore::where(['store_id' => $order->store_id,'is_lock' => 0])->field('gift_id')->find();
        if(empty($store)){
            return;
        }
        if(empty($store->gift)){
            return;
        }
        $coupon = Coupon::where(['id' => $store->gift->coupon_id])->find();
        if(empty($coupon)) {
           return;
        }
        //判断优惠券库存
        //判断用户是否会员(不是会员禁止增加记录)
        if($store->gift->types){
            $vip = Vip::where(['user_id' => $order->uid,'is_lock' => 0])->field('vipcard_id')->find();           
            if(empty($vip)){
                Inform::sms($order->uid,$miniapp_id,['title' =>'业务进展通知','type' => '支付有礼','content' =>'开通会员买单有机会活动获得['.$coupon['name'].']','state' => '无身份']);
                return;
            }
        }
        //增加与更新付款数据
        $giftData = self::where(['uid' => $order->uid,'gift_id' => $store->gift->id])->find();
        //发放优惠券
        if(empty($giftData)){
            $data['uid']        = $order->uid;
            $data['gift_id']    = $store->gift->id;
            $data['pay_num']    = 1;
            $data['pay_amount'] = $order->order_amount;
            self::insert($data);
        }else{
            //判断是否已领取（如果需要判断领取次数的限制也在这里增加判断代码）
            if($giftData->coupon_id > 0){
                if($order->coupon_user_id == $giftData->coupon_id){
                    $giftData->is_take   = 1;
                    $giftData->take_time = Request::time();
                    $giftData->save();
                }
                return; //暂停执行后面的代码
            }
            $giftData->pay_num    = ['inc',1];
            $giftData->pay_amount = ['inc',$order->order_amount];
            $giftData->save();
        }
        $info = self::where(['uid' => $order->uid,'gift_id' => $store->gift->id])->field('pay_num,pay_amount,coupon_id,update_time')->find();
        if($store->gift->pay_num > 0 && $info->pay_num < $store->gift->pay_num){
            //通知申请者到微信
            Inform::sms($order->uid,$miniapp_id,['title' =>'业务进展通知','type' => '支付有礼','content' =>'您还需要再支付' . $store->gift->pay_amount - $info->pay_amount . '次获得'.$coupon->name,'state' => '待完成']);
            return;
        }
        if($store->gift->pay_amount > 0 && $info->pay_amount < $store->gift->pay_amount){
            //通知申请者到微信
            Inform::sms($order->uid,$miniapp_id,['title' =>'业务进展通知','type' => '支付有礼','content' =>'您还需要再支付' . $store->gift->pay_amount - $info->pay_amount . '元获得'.$coupon->name,'state' => '待完成']);
            return;
        }
        //发放指定优惠券券给用户
        $coupon_ids = json_encode([$store->gift->coupon_id]);
        $rel = CouponUser::addUserCoupon($coupon_ids,$order->uid); //领取的优惠券
        if(!empty($rel)){
            $info->coupon_id   = $store->gift->coupon_id;
            $info->update_time = Request::time();
            $info->save();
        }
        if($coupon){
            //通知申请者到微信
            Inform::sms($order->uid,$miniapp_id,['title' =>'业务进展通知','type' => '支付有礼','content' =>'恭喜您获得'.$coupon->name,'state' => '成功']);
        }
    }
}